package com.fzkjjt.order.service;

import com.fzkjjt.order.mapper.Order;
import com.fzkjjt.order.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

/**
 * @program: seata-simple
 * @description:
 * @author: FangZhen
 * @create: 2020-10-14 15:32
 **/
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @GlobalTransactional
    public void create(String userId, String commodityCode, int orderCount) {
        String storageURL = "http://127.0.0.1:8083/deduct";
        restTemplate.getForEntity(storageURL, Void.class);

        String accountURL = "http://127.0.0.1:8081/debit";
        restTemplate.getForEntity(accountURL, Void.class);

        Order order = Order.builder().userId(userId).commodityCode(commodityCode).count(orderCount).money(5).build();
        orderMapper.insert(order);
        throw new NullPointerException();
    }
}
